<template>
  <div>
    <a-page-header title="我的订单" @back="() => $router.go(-1)"/>
    <a-list item-layout="horizontal" :data-source="concreteOrder" :bordered="true" v-if="timeToShowList">
      <a-list-item slot="renderItem" slot-scope="item, index">
        <a :href="'/#/order?orderID='+item.orderID" slot="actions">￥{{item.billSum}}<br/>共{{calThingSum(item.cart)}}件</a>
        <div :href="'/#/order?orderID='+item.orderID" slot="extra">{{getOrderStatus(item.orderState)}}</div>
        <a :href="'/#/order?orderID='+item.orderID" style="width: 100%">
          <a-list-item-meta>
            <a-avatar :src="sellerInfoArray[item.sellerID].logo" slot="avatar"/>
              <h3 style="font-weight: bold" slot="title">{{sellerInfoArray[item.sellerID].name}}</h3>
            <div slot="description">
              <div v-for="dish in item.cart">{{dish.thingName+' x'+dish.thingNumber}}</div>
            </div>
          </a-list-item-meta>
        </a>
      </a-list-item>
    </a-list>
    <a-skeleton :loading="!timeToShowList" active/>
  </div>
</template>

<script>
import {notification} from "ant-design-vue";

export default {
name: "myorder",
  methods:{
  // calPriceSum:function (orderID){
  //   var PriceSum=0
  //   for(var i=0;i<50;i++)
  //     PriceSum=PriceSum+this.orderData_end[orderID].cart[i].singlePrice
  //   return PriceSum
  // },
    calThingSum:function (cart){
      let ThingSum = 0;
      cart.forEach((item)=>{
         ThingSum+=parseInt(item.thingNumber)
      })
      return ThingSum
    },
    getMyOrders:function(){
      let rearUrl=this.$store.state.rear_url
      this.concreteOrder=[]
      this.$axios.post('http://'+rearUrl+'/myorder',{
        userID:this.$store.state.userID,
        token:this.$store.state.token
      }).then((res)=>{
        //console.log(res.data)
        if(res.data.myorderInfoStatus!==0) {
          //说明用户没有订单或token校验不成功
          this.timeToShowList=true
          if(res.data.myorderInfoStatus==2){
            notification.open({
              message:'用户信息不完整',
              description:"请重新登录饿死了么账号"
            })
            window.location='/#/login'
            return
          }
        }else{
          this.unloadedOrders=res.data.myorderList.length
          res.data.myorderList.forEach((idii,indexx)=>{
            this.$axios.post('http://'+rearUrl+'/myorder1',{
              orderID:idii,
              userID:this.$store.state.userID,
              token:this.$store.state.token
            }).then((response)=>{
              //console.log(response.data)
              if(response.data.myorderInfoStatus!==0){
                if(response.data.myorderInfoStatus==2){
                  notification.open({
                    message:'用户信息不完整',
                    description:"请重新登录饿死了么账号"
                  })
                  window.location='/#/login'
                  return
                }
                notification.open({
                  message: '请求错误',
                  description: '请求订单信息时服务器返回了状态码：' + response.data.myorderInfoStatus
                })
              }else{
                this.concreteOrder.push(response.data)
                this.getSellerInfo(response.data.sellerID)
              }
            })
          })
        }
      })
    },
    getSellerInfo:function (sellerID){
      if(this.queueSeller[sellerID]!=undefined) {
        this.unloadedOrders--
        if(this.unloadedOrders<=0) this.sortOrders()
        return
      }
      this.queueSeller[sellerID]=1
      let rearUrl=this.$store.state.rear_url
      this.$axios.post('http://'+rearUrl+'/sellerinfo',{
        sellerID:sellerID
      }).then((response)=>{
            if(response.data.sellerInfoStatus!==0){
              notification.open({
                message:'请求商家信息失败',
                description:'请求商家信息时服务器返回了状态码1'
              })
            }else{
              this.sellerInfoArray[sellerID]= response.data
              this.unloadedOrders--
              if(this.unloadedOrders<=0) this.sortOrders()
            }
          }).catch((err)=>{
        notification.open({
          message:'请求商家信息失败',
          description:"服务器错误："+err.message
        })
      })
    },
    getOrderStatus:function (state){
      switch (state){
        case 0:
          return '等待用户付款'
        case 1:
          return '已付款，等待商家接单'
        case 2:
          return '商家已接单，等待骑手配送'
        case 3:
          return '骑手正在配送，请耐心等待'
        case 4:
          return '订单已完成'
        default:
          return '订单状态异常：'+state
      }
    },
    //createTime: "2020-09-20 16:55:03"
    sortOrders:function (){
      //将订单按创建时间倒序排列
      this.concreteOrder.sort(function (a,b){
        return -a.createTime.localeCompare(b.createTime)
      })
      this.timeToShowList=true
    }
  },
  data:function (){
    return{
      concreteOrder:[],
      sellerInfoArray:[],
      queueSeller:[],
      unloadedOrders:0,
      timeToShowList:false
    }
  },
  created:function (){
    if(!this.$store.getters.userInfoComplete) {
      notification.open({
        message:'用户信息不完整',
        description:"请重新登录饿死了么账号"
      })
      window.location='/#/login'
      return
    }
    this.getMyOrders()
  }
}
</script>

<style scoped>

</style>